package com.cchip.cloud.api;

import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

import com.browan.freeppmobile.jni.FreePPJNI;
import com.browan.freeppmobile.jni.FreePPJNI.StatusListener;
import com.cchip.cloud.system.Account;
import com.cchip.cloud.system.Config;

public class RsApi implements StatusListener {
	private static final String TAG = "FreeppKit";

	private static RsApi instance = null;
	private static FreePPJNI kit = null;
	private int callNumber = 0;
	// private int regNumber = 0;

	public static String freeppId = null;
	public static String e164Number = null;

	public static boolean needRingback = false;
	public static boolean inMeeting = false;

	static {
		System.loadLibrary("stlport_shared");
		System.loadLibrary("VoiceEngineJni");
	}

	public static RsApi getInstance() {
		if (instance == null)
			instance = new RsApi();

		return instance;
	}

	private RsApi() {
		kit = new FreePPJNI();

	}

	public void init() {
		if (kit != null) {
			kit.initialize(this);
			kit.setcallerid(Account.my.name, Account.my.roomId);
		}
	}

	public void destroy() {
		if (kit != null)
			kit.shutdown();
	}

	public void dial(String calleeId) {

		String user = Account.my.roomId + ":" + Account.my.password;
		// String callId = getFreeppId() + "-" + System.currentTimeMillis();
		// String cmd = String.format("[mobile@%s]%s", callId, number);

		callNumber = kit.dail(calleeId, Config.getRsHost(), user, "");
	}

	// public void register()
	// {
	// regNumber = kit.register(e164Number, freeppId, "11111111",
	// Config.getRsHost());
	// }
	//
	// public void unregister()
	// {
	// kit.unregister(regNumber);
	// }

	// public void answer() {
	// kit.answer(callNumber);
	// }

	public void hangup() {
		if (kit != null)
			kit.hangup(callNumber);
	}

	public void setMute(boolean mute) {
		if (kit != null)
			kit.mute(callNumber, mute ? 1 : 0);
	}

	@Override
	public void onEvent(int type, String jsonString) {
		Log.i(TAG, "Type: " + type);
		Log.i(TAG, jsonString);

		jsonString = jsonString.replace("[", "^");
		jsonString = jsonString.replace("]", "*");
		try {
			JSONObject obj = new JSONObject(jsonString);
			switch (type) {
			case 0: // EVENT_REGISTRATION
				Log.i(TAG, "Iax Registered");
				break;
			case 1: // EVENT_STATE
				int curState = obj.getInt("activity");

				switch (curState) {
				case 0: // CALL_FREE
					Log.i(TAG, "Iax hangup");
					break;
				case 2: // CALL_RINGIN
					Log.i(TAG, "Iax ringin");
					callNumber = obj.getInt("callNo");
					// String cmd = obj.getString("peername");
					//
					// int idx1 = cmd.indexOf("@");
					// int idx2 = cmd.indexOf("*");
					// String callId = cmd.substring(idx1 + 1, idx2);
					break;
				case 1: // CALL_OUTGOING
				case 3: // CALL_RINGBACK
					// do nothing
					break;
				case 4: // CALL_ANSWERED
					Log.i(TAG, "Iax Answered");
					break;
				}

				break;
			}
		} catch (JSONException e) {
			// Print.e(TAG, "Freepp Engine Callback Error");
		}
	}
}
